//
//  LYFdiscoverTableViewCell.m
//  段子手
//
//  Created by dllo on 16/3/17.
//  Copyright © 2016年 JokesTeam. All rights reserved.
//

#import "LYFdiscoverTableViewCell.h"

#import "JCAlertView.h"

@interface LYFdiscoverTableViewCell ()

@property (nonatomic, retain) UIImageView *imgView;/**< 左边图片 */
@property (nonatomic, retain) UILabel *titleLabel;/**< 上面标题 */
@property (nonatomic, retain) UILabel *smallLabel;/**< 中间内容文字 */
@property (nonatomic, retain) UILabel *subscriptionLabel;/**< 订阅label */
@property (nonatomic, retain) UILabel *totalPostsLabel;/**< 总帖数label */
@property (nonatomic, retain) UIButton *subscriptionButton;/**< 订阅button */

@end

@implementation LYFdiscoverTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createCell];
    }
    return self;
}



- (void)createCell {
    self.imgView = [[UIImageView alloc] init];
    [self.contentView addSubview:_imgView];
    
    self.titleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_titleLabel];
    
    self.smallLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_smallLabel];
    
    self.subscriptionLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_subscriptionLabel];
    
    self.totalPostsLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_totalPostsLabel];
    
    self.subscriptionButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_subscriptionButton];
}

- (void)setModel2:(LYFDiscoverModel *)model2 {
    _model2 = model2;
    
    [_imgView sd_setImageWithURL:[NSURL URLWithString:_model2.icon_url]];
    
    _titleLabel.text = _model2.name;
    
    _smallLabel.text = _model2.placeholder;
    
    
    _subscriptionLabel.text = [NSString stringWithFormat:@"%ld订阅", _model2.subscribe_count];
    _totalPostsLabel.text = [NSString stringWithFormat:@"总帖数%ld", _model2.total_updates];
    _totalPostsLabel.textColor = [UIColor colorWithRed:1.000 green:0.400 blue:0.400 alpha:1.000];
}




- (void)layoutSubviews {
    [super layoutSubviews];
    _imgView.frame = CGRectMake(10, 10, 80, 80);
//    _imgView.backgroundColor = [UIColor yellowColor];
    
    _titleLabel.frame = CGRectMake(100, 10, SCREEN_WIDTH / 2, 30);
    _titleLabel.font = [UIFont systemFontOfSize:15];
//    _titleLabel.backgroundColor = [UIColor greenColor];
    
    _smallLabel.frame = CGRectMake(100, 40, SCREEN_WIDTH - 200, 25);
    _smallLabel.font = [UIFont systemFontOfSize:13];
    _smallLabel.textColor = [UIColor grayColor];
//    _smallLabel.backgroundColor = [UIColor grayColor];
    
    _subscriptionLabel.frame = CGRectMake(100, 65, 100, 25);
    _subscriptionLabel.font = [UIFont systemFontOfSize:13];
    _subscriptionLabel.textColor = [UIColor grayColor];
//    _subscriptionLabel.backgroundColor = [UIColor purpleColor];
    
    _totalPostsLabel.frame = CGRectMake(200, 65, 100, 25);
    _totalPostsLabel.font = [UIFont systemFontOfSize:13];
//    _totalPostsLabel.textColor = [UIColor grayColor];
//    _totalPostsLabel.backgroundColor = [UIColor orangeColor];
    
    _subscriptionButton.frame = CGRectMake(SCREEN_WIDTH - 50, 40, 40, 20);
    _subscriptionButton.backgroundColor = [UIColor grayColor];
    _subscriptionButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _subscriptionButton.layer.cornerRadius = 5;
    [_subscriptionButton setTitle:@"订阅" forState:UIControlStateNormal];
    [_subscriptionButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
}


/** 订阅button点击方法 */
- (void)buttonAction:(UIButton *) button {
    
    
    
    [[LYFDataBaseHandlle sharedDataBaseHandle] openDB];
    [[LYFDataBaseHandlle sharedDataBaseHandle] createTable];
    
    NSMutableArray *arrName = [NSMutableArray array];
    NSMutableArray *arrModel = [[LYFDataBaseHandlle sharedDataBaseHandle] selectTable];
    for (LYFDiscoverModel *model in arrModel) {
        [arrName addObject:model.name];
    }
    
    if ([arrName containsObject:_model2.name]) {
        [JCAlertView showOneButtonWithTitle:@"提示" Message:@"该专题已被订阅" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"确定" Click:^{
            
        }];

        
    } else {
        
        [[LYFDataBaseHandlle sharedDataBaseHandle] insertModel:_model2];
        
        [JCAlertView showOneButtonWithTitle:@"提示" Message:@"订阅成功" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"确定" Click:^{
            
        }];
        
    }
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
